package com.sjzd.meeting.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ControllerExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public void exceptionHandle(HttpServletRequest req, Exception e) throws Exception {
        log.error("Request url : {}, method : {}, exception ： {}",
                req.getRequestURL(), req.getMethod(), e);
        // 如果已经定义了 ResponseStatus 标签来处理异常，那么我们希望这个异常交给springboot处理
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
            throw e;
        }
    }
}
